package card_discount

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/internal/service/param"
	"context"
)

// ListItemConfigurationPtrsByCardDiscountID 获取卡折扣到商品配置数组
func (s *CardDiscountEntityServiceImp) ListItemConfigurationPtrsByCardDiscountID(ctx context.Context, q *sqlc.Queries, cardDiscountID int64) (arr param.ItemConfigurationPtrArray, err error) {
	cardDiscountItems, err := q.ListNormalCardDiscountItemsByCardDiscountID(ctx, cardDiscountID)
	if err != nil {
		return
	}
	for _, cardDiscountItem := range cardDiscountItems {
		arr = append(arr, &param.ItemConfiguration{
			ID:   cardDiscountItem.ItemID,
			Name: cardDiscountItem.ItemName,
		})
	}
	return
}

// ListItemIDsByCardDiscountID 获取卡折扣到商品IDs
func (s *CardDiscountEntityServiceImp) ListItemIDsByCardDiscountID(ctx context.Context, q *sqlc.Queries, cardDiscountID int64) (itemIDs []param.ItemID, err error) {
	cardDiscountItems, err := q.ListNormalCardDiscountItemsByCardDiscountID(ctx, cardDiscountID)
	if err != nil {
		return
	}
	for _, cardDiscountItem := range cardDiscountItems {
		itemIDs = append(itemIDs, param.ItemID(cardDiscountItem.ItemID))
	}
	return
}

// GetMapOfCardDiscountIDToItemConfigurationPtGetMapOfCardDiscountIDToItemConfigurationPtrsByCardDiscountIDsrs 获取优惠券到商品配置的映射
func (s *CardDiscountEntityServiceImp) GetMapOfCardDiscountIDToItemConfigurationPtrsByCardDiscountIDs(ctx context.Context, q *sqlc.Queries, cardDiscountIDs []int64) (m param.EntityIDToItemConfigurationPtrsMap, err error) {
	m = make(param.EntityIDToItemConfigurationPtrsMap)
	if len(cardDiscountIDs) == 0 {
		return
	}
	cardDiscountItems, err := q.ListNormalCardDiscountItemsByCardDiscountIDs(ctx, cardDiscountIDs)
	if err != nil {
		return
	}
	for _, cardDiscountItem := range cardDiscountItems {
		cardDiscountID := cardDiscountItem.CardDiscountID
		valPtr := &param.ItemConfiguration{
			ID:   cardDiscountItem.ItemID,
			Name: cardDiscountItem.ItemName,
		}
		if val, ok := m[cardDiscountID]; ok {
			val = append(val, valPtr)
			m[cardDiscountID] = val
		} else {
			m[cardDiscountID] = []*param.ItemConfiguration{valPtr}
		}
	}
	return
}

// GetMapOfCardDiscountIDToItemConfigurationPtrsByCardDiscountPtrs 获取优惠券到商品配置的映射
func (s *CardDiscountEntityServiceImp) GetMapOfCardDiscountIDToItemConfigurationPtrsByCardDiscountPtrs(ctx context.Context, q *sqlc.Queries, ptrs []*sqlc.CardDiscount) (m param.EntityIDToItemConfigurationPtrsMap, err error) {
	return s.GetMapOfCardDiscountIDToItemConfigurationPtrsByCardDiscountIDs(ctx, q, s.ConvToCardDiscountIDs(ptrs))
}

// GetMapOfCardDiscountIDToItemConfigurationPtrsByListCardDiscountWithStatRowPtrs 获取优惠券到商品配置的映射
func (s *CardDiscountEntityServiceImp) GetMapOfCardDiscountIDToItemConfigurationPtrsByListCardDiscountWithStatRowPtrs(ctx context.Context, q *sqlc.Queries, ptrs []*sqlc.ListCardDiscountWithStatsRow) (m param.EntityIDToItemConfigurationPtrsMap, err error) {
	return s.GetMapOfCardDiscountIDToItemConfigurationPtrsByCardDiscountIDs(ctx, q, s.ConvToCardDiscountIDs(ptrs))
}